﻿using IdentityServer4.Common;
using IdentityServer4.Models;
using IdentityServer4.Test;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace IdentityServer4.ResourceOwnerPassword.Server {
    public class Config {

        /// <summary>
        /// 身份资源，控制身份令牌包含的信息
        /// </summary>
        public static IEnumerable<IdentityResource> IdentityResources =>
            new IdentityResource[]
            {
                new IdentityResources.OpenId(),
            };

        /// <summary>
        ///  授权范围
        /// </summary>
        public static IEnumerable<ApiScope> ApiScopes =>
            new ApiScope[]
            {
                new ApiScope("scope1","api授权范围1"),
                new ApiScope("scope2","api授权范围2"),
            };

        /// <summary>
        /// 保护的API资源
        /// </summary>
        public static IEnumerable<ApiResource> ApiResources =>
            new ApiResource[]
            {
                new ApiResource(ApiResourceNames.Api1,"#api1")
                {
                    //4.X版本增加!!!重要,
                    Scopes = { "scope1" }//授权范围
                },
                new ApiResource(ApiResourceNames.Api2,"#api2")
                {
                    //!!!重要
                    Scopes = { "scope2"}
                },
            };

        /// <summary>
        /// 客户端配置
        /// </summary>
        public static IEnumerable<Client> Clients =>
            new Client[]
            {
                new Client
                {
                    ClientId = "client", //客户端id
                    ClientName = "客户端",

                    //配置授权类型，可以配置多个授权类型
                    AllowedGrantTypes = new List<string>()
                    {
                        GrantTypes.ResourceOwnerPassword.FirstOrDefault(),//资源所有者密码授权
                    },
                    ClientSecrets = { new Secret("secret".Sha256()) }, //客户端加密方式

                    //配置授权范围
                    AllowedScopes = { "scope1" },

                    AccessTokenLifetime = 360000,//配置Token 失效时间，毫秒
                },
            };

        /// <summary>
        /// 测试用户及其身份信息单元
        /// </summary>
        /// <returns></returns>
        public static List<TestUser> GetUsers() =>
            new List<TestUser>
            {
                new TestUser
                {
                    SubjectId = "1",
                    Username = "username1",
                    Password = "password1"
                },
                new TestUser
                {
                    SubjectId = "2",
                    Username = "username2",
                    Password = "password2"
                }
            };
    }
}
